package com.qdsgjsfk.vision.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.base.BaseDialogFragment;

import java.util.Objects;

import io.reactivex.annotations.NonNull;


/**
 * Description
 * Created by lbj on 2021/11/15 12:59
 */

public class UpdateDialog extends BaseDialogFragment {

    public static UpdateDialog newInstance(int isForceUpdate, boolean noUpdate) {
        Bundle args = new Bundle();
        args.putInt( "isForceUpdate", isForceUpdate );
        args.putBoolean( "noUpdate", noUpdate );
        UpdateDialog fragment = new UpdateDialog();
        fragment.setArguments( args );
        return fragment;
    }


    private Dialog mDialog;
    private OnItemClickListener mListener;

    @SuppressLint("WrongViewCast")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.dialog_update, container );
        TextView tv_tips = view.findViewById( R.id.tv_tips );
        TextView tv_message = view.findViewById( R.id.tv_message );
        TextView btn_confirm = view.findViewById( R.id.btn_confirm );

        TextView btn_cancel = view.findViewById( R.id.btn_cancel );
        btn_cancel.setOnClickListener( v -> {
            if (mListener != null) {
                mListener.onCancel();
                dismiss();
            }
        } );

        btn_confirm.setOnClickListener( v -> {
            if (mListener != null) {
                mListener.onConfirm();
                dismiss();
            }
        } );
        assert getArguments() != null;
        if (getArguments().getInt( "isForceUpdate" ) == 1) {
            //强制更新
            btn_cancel.setVisibility( View.GONE );
        }
        boolean noUpdate = getArguments().getBoolean( "noUpdate" );
        if (noUpdate) {
            tv_tips.setText( "温馨提示" );
            tv_message.setText( "确定要删除该处方？" );
        }

        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );


    }


    @android.support.annotation.NonNull
    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog( savedInstanceState );
        mDialog.setCancelable( false );
        mDialog.setCanceledOnTouchOutside( false );
        mDialog.setOnKeyListener( keylistener );
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity( Gravity.CENTER );
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Objects.requireNonNull( Objects.requireNonNull( getDialog() ).getWindow() ).requestFeature( Window.FEATURE_NO_TITLE );
        super.onActivityCreated( savedInstanceState );
        Objects.requireNonNull( getDialog().getWindow() ).setBackgroundDrawable( new ColorDrawable( 0x00000000 ) );
        getDialog().getWindow().setLayout( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT );
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm();

        void onCancel();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


    private DialogInterface.OnKeyListener keylistener = (dialog, keyCode, event) -> keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0;

}
